
package com.contractmodule.dao;
import com.contractmodule.bean.*;
import java.sql.*;
import java.io.*;
import java.util.*;
import util.*;
public class TbAccessoriesDAO {
    private Connection conn = null;
    private PreparedStatement pt = null;
    private ResultSet rs = null;
    private boolean flag = false;
    private String sql = "";
    private TbAccessories tbAccessories=null;
    private List<TbAccessories> tbAccessoriesList=null;

    public boolean add(TbAccessories inTbAccessories){
        try{
            
        conn=ConnectDB.getSqlServerConnection();
        sql="insert into Tb_Accessories(ContractCode,AccessoriesFileName,UserId) values(?,?,?)";
        pt = conn.prepareStatement(sql);
        pt.setString(1,inTbAccessories.getContractCode());
        pt.setString(2,inTbAccessories.getAccessoriesFileName());
        pt.setInt(3,inTbAccessories.getUserId());
        if (pt.executeUpdate() > 0) flag=true;
        }catch(Exception e){
            e.printStackTrace();
        }finally {
                        ConnectDB.closeSqlConnection();        }
        return flag;
    }
    public boolean del(int inAccessoriesId){
        return UtilDAO.del("Tb_Accessories", "AccessoriesId", inAccessoriesId);
    }
    public boolean edit(TbAccessories inTbAccessories){
        try{
            
        conn=ConnectDB.getSqlServerConnection();
        sql="update Tb_Accessories set ContractCode=?,AccessoriesFileName=?,UserId=?,CreateTime=getdate() where AccessoriesId=?";
        pt = conn.prepareStatement(sql);
        pt.setString(1,inTbAccessories.getContractCode());
        pt.setString(2,inTbAccessories.getAccessoriesFileName());
        pt.setInt(3,inTbAccessories.getUserId());
        pt.setInt(4,inTbAccessories.getAccessoriesId());
        if (pt.executeUpdate() > 0) flag=true;
        }catch(Exception e){
            e.printStackTrace();
        }finally {
                        ConnectDB.closeSqlConnection();        }
        return flag;
    }
    public TbAccessories getTbAccessoriesByAccessoriesId(int inAccessoriesId) {
        tbAccessories=null;
        try{
            conn = ConnectDB.getSqlServerConnection();
            sql = "select * from Tb_Accessories where AccessoriesId=? ";
            pt = conn.prepareStatement(sql);
            pt.setInt(1,inAccessoriesId);
            rs = pt.executeQuery();
            while (rs.next()) {
                tbAccessories = getTbAccessories(rs);
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
                        ConnectDB.closeSqlConnection();        }
        return tbAccessories;
    }
    public List<TbAccessories> getTbAccessoriesAll() {
        tbAccessoriesList=new ArrayList<TbAccessories>();
        try{
            conn = ConnectDB.getSqlServerConnection();
            sql = "select * from Tb_Accessories";
            pt = conn.prepareStatement(sql);
            rs = pt.executeQuery();
            while (rs.next()) {
                tbAccessoriesList.add(getTbAccessories(rs));
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
                        ConnectDB.closeSqlConnection();        }
        return tbAccessoriesList;
    }
    public List<TbAccessories> getTbAccessoriesAll(VskPage vp) {
        tbAccessoriesList=new ArrayList<TbAccessories>();
        try{
            conn = ConnectDB.getSqlServerConnection();
            sql = "select * from Tb_Accessories";
            vp.setAllCountSql(sql);
            vp.getAllRecordCount(conn, vp.getAllCountSql());
            pt = conn.prepareStatement(vp.processPageSql( "Tb_Accessories", "AccessoriesId"));
            rs = pt.executeQuery();
            while (rs.next()) {
                tbAccessoriesList.add(getTbAccessories(rs));
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
                        ConnectDB.closeSqlConnection();        }
        return tbAccessoriesList;
    }
    /**
     * 根据合同的编号得到其下的所有附件
     *@author qingyu zhang
     *@function:
     * @param contractCode
     * @return
     *2011-6-29
     *下午05:08:13
     *ProjectManagerT
     *com.contractmodule.dao
     *List<TbAccessories>
     */
    public List<TbAccessories> getAccessoriesList(String contractCode){
    	tbAccessoriesList = new ArrayList<TbAccessories>();
    	 try {
			conn = ConnectDB.getSqlServerConnection();
			sql = "select * from Tb_Accessories where ContractCode ='"+contractCode+"'";
			System.out.println("sql==="+sql);
			pt = conn.prepareStatement(sql);
			rs = pt.executeQuery();
			while(rs.next()){
				tbAccessoriesList.add(this.getTbAccessories(rs));
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
                        ConnectDB.closeSqlConnection();        }
        return tbAccessoriesList;
    }
    public TbAccessories getTbAccessories(ResultSet rs) throws Exception{
        TbAccessories tbAccessories = new TbAccessories();
        tbAccessories.setAccessoriesId(rs.getInt("AccessoriesId"));
        tbAccessories.setContractCode(rs.getString("ContractCode"));
        tbAccessories.setAccessoriesFileName(rs.getString("AccessoriesFileName"));
        tbAccessories.setUserId(rs.getInt("UserId"));
        tbAccessories.setCreateTime(rs.getString("CreateTime"));
        return tbAccessories;
    }
}
